<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="100px">
      <el-row :gutter="24">
        <el-col :md="6" :sm="12">
          <el-form-item label="订单号" prop="orderNum">
            <el-input
                v-model="queryParams.orderNum"
                placeholder="请输入订单号"
                clearable
                @keyup.enter.native="handleQuery"
            />
          </el-form-item>
        </el-col>
        <el-col :md="6" :sm="12">
          <el-form-item label="车牌号" prop="carNum">
            <el-input
                v-model="queryParams.carNum"
                placeholder="请输入车牌号"
                clearable
                @keyup.enter.native="handleQuery"
            />
          </el-form-item>
        </el-col>
        <el-col :md="6" :sm="12">
          <el-form-item label="状态" prop="status">
            <el-select v-model="queryParams.status" placeholder="请选择0 待确认 1 未处理  2已处理 3 确认未违章 4已结算"
                       clearable>
              <el-option
                  v-for="dict in dict.type.violation_status"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :md="6" :sm="12">
          <el-form-item style="margin-bottom: 20px">
            <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
            <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
            <a style="margin-left: 8px" @click="toggleAdvanced">
              {{ advanced ? '收起' : '展开' }}
              <el-icon :class="advanced?'el-icon-arrow-up':'el-icon-arrow-down'"/>
            </a></el-form-item>
        </el-col>
        <el-collapse-transition>
          <div v-show="advanced">
            <el-col :md="6" :sm="12">
              <el-form-item label="订单id" prop="orderId">
                <el-input
                    v-model="queryParams.orderId"
                    placeholder="请输入订单id"
                    clearable
                    @keyup.enter.native="handleQuery"
                />
              </el-form-item>
            </el-col>
            <el-col :md="6" :sm="12">
              <el-form-item label="车辆id" prop="vehicleId">
                <el-input
                    v-model="queryParams.vehicleId"
                    placeholder="请输入车辆id"
                    clearable
                    @keyup.enter.native="handleQuery"
                />
              </el-form-item>
            </el-col>
            <el-col :md="6" :sm="12">
              <el-form-item label="违章时间" prop="violationTime">
                <el-date-picker clearable
                                v-model="queryParams.violationTime"
                                type="date"
                                value-format="yyyy-MM-dd"
                                placeholder="请选择违章时间">
                </el-date-picker>
              </el-form-item>
            </el-col>
            <el-col :md="6" :sm="12">
              <el-form-item label="处理方式" prop="handleMethod">
                <el-select v-model="queryParams.handleMethod" placeholder="请选择1 扣款处理 2 客户处理" clearable>
                  <el-option
                      v-for="dict in dict.type.violation_handler_method"
                      :key="dict.value"
                      :label="dict.label"
                      :value="dict.value"
                  />
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :md="6" :sm="12">
              <el-form-item label="录入人的姓名" prop="entryName">
                <el-input
                    v-model="queryParams.entryName"
                    placeholder="请输入录入人的姓名"
                    clearable
                    @keyup.enter.native="handleQuery"
                />
              </el-form-item>
            </el-col>
            <el-col :md="6" :sm="12">
              <el-form-item label="处理人的姓名" prop="confirmName">
                <el-input
                    v-model="queryParams.confirmName"
                    placeholder="请输入确认人的姓名"
                    clearable
                    @keyup.enter.native="handleQuery"
                />
              </el-form-item>
            </el-col>
            <el-col :md="6" :sm="12">
              <el-form-item label="录入人的ID" prop="entryUserId">
                <el-input
                    v-model="queryParams.entryUserId"
                    placeholder="请输入录入人的ID"
                    clearable
                    @keyup.enter.native="handleQuery"
                />
              </el-form-item>
            </el-col>
            <el-col :md="6" :sm="12">
              <el-form-item label="处理人的id" prop="confirmUserId">
                <el-input
                    v-model="queryParams.confirmUserId"
                    placeholder="请输入确认人的id"
                    clearable
                    @keyup.enter.native="handleQuery"
                />
              </el-form-item>
            </el-col>
            <el-col :md="6" :sm="12">
              <el-form-item label="门店" prop="deptId">
                <el-select v-model="form.deptId"
                           value-key="deptId"
                           clearable placeholder="请选择门店">
                  <el-option
                      v-for="item in branchOptions"
                      :key="item.deptId"
                      :label="item.name"
                      :value="item"
                      :disabled="item.status == 0"
                  ></el-option>
                </el-select>
              </el-form-item>
            </el-col>
          </div>
        </el-collapse-transition>

      </el-row>

    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
            type="primary"
            plain
            icon="el-icon-plus"
            size="mini"
            @click="handleAdd"
            v-hasPermi="['merchant:violation:add']"
        >新增
        </el-button>
      </el-col>
<!--      <el-col :span="1.5">-->
<!--        <el-tooltip class="item" effect="dark" content="确认未违章与已处理违章记录将不能修改" placement="top">-->
<!--          <el-button-->
<!--              type="success"-->
<!--              plain-->
<!--              icon="el-icon-edit"-->
<!--              size="mini"-->
<!--              :disabled="single"-->
<!--              @click="handleUpdate"-->
<!--              v-hasPermi="['merchant:violation:edit']"-->
<!--          >修改-->
<!--          </el-button>-->
<!--        </el-tooltip>-->
<!--      </el-col>-->
      <el-col :span="1.5">
        <el-button
            type="danger"
            plain
            icon="el-icon-delete"
            size="mini"
            :disabled="multiple"
            @click="handleDelete"
            v-hasPermi="['merchant:violation:remove']"
        >删除
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
            type="warning"
            plain
            icon="el-icon-download"
            size="mini"
            @click="handleExport"
            v-hasPermi="['merchant:violation:export']"
        >导出
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="violationList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="主键id" align="center" prop="id"/>
      <el-table-column label="车辆id" align="center" prop="vehicleId"/>
      <el-table-column label="订单号" align="center" prop="orderNum" width="180"/>
      <el-table-column label="车牌号" align="center" prop="carNum" width="100"/>
      <el-table-column label="状态" align="center" prop="status" width="100">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.violation_status" :value="scope.row.status"/>
        </template>
      </el-table-column>
      <el-table-column label="违章时间" align="center" prop="violationTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.violationTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="车辆违章图片" align="center" prop="vehViolationPics" width="150">
        <template slot-scope="scope">
          <image-preview :src="scope.row.vehViolationPics" :width="50" :height="50"/>
        </template>
      </el-table-column>
      <el-table-column label="违章已处理证明图片" align="center" prop="vioHandledPics" width="150">
        <template slot-scope="scope">
          <image-preview :src="scope.row.vioHandledPics" :width="50" :height="50"/>
        </template>
      </el-table-column>
      <el-table-column label="车辆违章描述" align="center" prop="vehViolationDesc"/>
      <el-table-column label="备注" align="center" prop="remark"/>
      <el-table-column label="处理方式" align="center" prop="handleMethod">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.violation_handler_method" :value="scope.row.handleMethod"/>
        </template>
      </el-table-column>
      <el-table-column label="录入人姓名" align="center" prop="entryName" width="100"/>
      <el-table-column label="处理人姓名" align="center" prop="confirmName" width="100"/>
      <el-table-column label="录入人的ID" align="center" prop="entryUserId" width="90"/>
      <el-table-column label="处理人的ID" align="center" prop="confirmUserId" width="90"/>
      <el-table-column label="门店组织架构id" align="center" prop="deptId"/>
      <el-table-column label="违章金额" align="center" prop="money"/>
      <el-table-column label="违章扣分" align="center" prop="deductPoints"/>
      <el-table-column label="用户id" align="center" prop="wechatUserId"/>
      <el-table-column label="操作" align="center" width="120" class-name="small-padding fixed-width" fixed="right">
        <template slot-scope="scope">
          <el-tooltip class="item" effect="dark" content="确认未违章与已处理违章记录将不能修改" placement="top">
            <el-button
                size="mini"
                type="text"
                icon="el-icon-edit"
                :disabled="scope.row.status==2||scope.row.status==3"
                @click="handleUpdate(scope.row)"
                v-hasPermi="['merchant:violation:edit']"
            >修改
            </el-button>
          </el-tooltip>
          <el-button
              size="mini"
              type="text"
              icon="el-icon-delete"
              @click="handleDelete(scope.row)"
              v-hasPermi="['merchant:violation:remove']"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
        v-show="total>0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
    />

    <!-- 添加或修改车辆违章信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="1000px" append-to-body>
      <el-steps :active="active" align-center v-if="openType=='add'">
        <el-step title="选择订单"
                 description="违章的信息需要选择用户正在使用或已经退租的订单进行违章信息的录入"></el-step>
        <el-step title="违章信息"
                 description="在车辆订单退租后会自动生成一条未确认的违章，违章信息也可以手动录入，但是订单15天结算时必须所有违章信息都已处理，否则将不予自动结算"></el-step>
      </el-steps>
      <div v-if="active==0&&openType=='add'">
        <el-table v-loading="order.loading" :data="order.vehorderList"
                  ref="vehorderList">
          <el-table-column label="订单号" align="center" prop="orderNum" width="200"/>
          <el-table-column label="下单时间" align="center" prop="createTime" width="100">
            <template slot-scope="scope">
              <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d} {h}:{i}') }}</span>
            </template>
          </el-table-column>
          <el-table-column label="车辆天单价" align="center" prop="money" width="90">
            <template slot-scope="scope">
              <span>￥{{ scope.row.money }}/天</span>
            </template>
          </el-table-column>
          <el-table-column label="车辆租金" align="center" prop="totalMoney" width="80">
            <template slot-scope="scope">
              <span>￥{{ scope.row.totalMoney }}</span>
            </template>
          </el-table-column>
          <el-table-column label="车辆押金" align="center" prop="depositMoney" width="80">
            <template slot-scope="scope">
              <span>￥{{ scope.row.depositMoney }}</span>
            </template>
          </el-table-column>
          <el-table-column label="服务类型" align="center" prop="type" width="80">
            <template slot-scope="scope">
              <dict-tag :options="dict.type.order_servicetype" :value="scope.row.orderType"/>
            </template>
          </el-table-column>
          <el-table-column label="门店名称" align="center" prop="shopName" width="110"/>
          <el-table-column label="车牌号" align="center" prop="carNum" width="90"/>
          <el-table-column label="车辆名称" align="center" prop="carname" width="80"/>
          <el-table-column label="取车时间" align="center" prop="startTime" width="100"/>
          <el-table-column label="还车时间" align="center" prop="endTime" width="100"/>
          <el-table-column label="总天数" align="center" prop="day" width="60"/>
          <!--      <el-table-column label="${comment}" align="center" prop="uniacid" />-->
          <el-table-column label="当前接单人ID" align="center" prop="uid" width="60"/>
          <el-table-column label="支付金额" align="center" prop="totalMoney">
            <template slot-scope="scope">
              <span>￥{{ scope.row.totalMoney }}</span>
            </template>
          </el-table-column>
          <el-table-column label="取车姓名" align="center" prop="username" width="75"/>
          <el-table-column label="取车电话" align="center" prop="tel" width="110"/>
          <el-table-column label="操作" fixed="right" align="center"
                           class-name="small-padding fixed-width" width="100">
            <template slot-scope="scope">
              <el-button
                  type="text"
                  icon="el-icon-edit"
                  size="mini"
                  @click="choseOrder(scope.row)"
              >选择
              </el-button>
            </template>
          </el-table-column>
        </el-table>
        <pagination
            v-show="order.total>0"
            :total="order.total"
            :page.sync="queryParamsOrder.pageNum"
            :limit.sync="queryParamsOrder.pageSize"
            @pagination="getOrderList"
        />
      </div>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px" v-if="active==1||openType=='edit'">
        <el-row :gutter="24">
          <!--          <el-col :md="12" :sm="12">-->
          <!--            <el-form-item label="车辆id" prop="vehicleId">-->
          <!--              <el-input v-model="form.vehicleId" placeholder="请输入车辆id"/>-->
          <!--            </el-form-item>-->
          <!--          </el-col>-->
          <!--          <el-col :md="12" :sm="12">-->
          <!--            <el-form-item label="订单id" prop="orderId">-->
          <!--              <el-input v-model="form.orderId" placeholder="请输入订单id"/>-->
          <!--            </el-form-item>-->
          <!--          </el-col>-->
          <el-col :md="12" :sm="12">
            <el-form-item label="车牌号" prop="carNum">
              <el-select v-model="form.carNum" disabled style="width: 100%">
                <el-option v-for="carInfo in carList" :value="carInfo.carnum"
                           :key="carInfo.id"
                           :label="carInfo.carnum">
                <span class="float-left">
                  车牌：{{ carInfo.carnum }}
                </span>
                  <span class="float-right">名称：{{ carInfo.name }}</span>
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :md="12" :sm="12">
            <el-form-item label="订单号" prop="orderNum">
              <el-input v-model="form.orderNum" readonly placeholder="请输入订单号"/>
            </el-form-item>
          </el-col>

          <el-col :md="12" :sm="12">
            <el-form-item label="门店" prop="deptId">
              <el-select v-model="form.deptId"
                         value-key="deptId"
                         disabled
                         clearable placeholder="请选择门店">
                <el-option
                    v-for="item in branchOptions"
                    :key="item.id"
                    :label="item.name"
                    :value="item.deptId"
                    :disabled="item.status == 0"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>

          <el-col :md="12" :sm="12">
            <el-form-item label="状态" prop="status">
              <el-select v-model="form.status"
                         placeholder="请选择0 待确认 1 未处理  2已处理 3 确认未违章" clearable>
                <el-option
                    v-for="dict in dict.type.violation_status"
                    :key="dict.value"
                    :label="dict.label"
                    :value="Number(dict.value)"/>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :md="12" :sm="12" v-if="form.status>0&&form.status<3">
            <el-form-item label="违章金额" prop="money">
              <el-input v-model="form.money" placeholder="请输入违章金额"/>
            </el-form-item>
          </el-col>
          <el-col :md="12" :sm="12" v-if="form.status>0&&form.status<3">
            <el-form-item label="扣分分数" prop="deductPoints">
              <el-input v-model.number="form.deductPoints" placeholder="请输入扣分分数"/>
            </el-form-item>
          </el-col>
          <el-col :md="12" :sm="12" v-if="form.status>0&&form.status<3">
            <el-form-item label="车辆违章图片" prop="vehViolationPics">
              <image-upload v-model="form.vehViolationPics"/>
            </el-form-item>
          </el-col>
          <el-col :md="12" :sm="12" v-if="form.status==2">
            <el-form-item label="车辆违章已处理证明图片" prop="vioHandledPics">
              <image-upload v-model="form.vioHandledPics"/>
            </el-form-item>
          </el-col>
          <el-col :md="12" :sm="12" v-if="form.status==2">
            <el-form-item label="处理方式" prop="handleMethod">
              <el-select v-model="form.handleMethod" placeholder="请选择1 扣款处理 2 客户处理">
                <el-option
                    v-for="dict in dict.type.violation_handler_method"
                    :key="dict.value"
                    :label="dict.label"
                    :value="parseInt(dict.value)"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>

          <el-col :md="12" :sm="12">
            <el-form-item label="违章时间" prop="violationTime">
              <el-date-picker clearable
                              v-model="form.violationTime"
                              type="date"
                              value-format="yyyy-MM-dd"
                              placeholder="请选择违章时间">
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :md="12" :sm="12">
            <el-form-item label="车辆违章描述" prop="vehViolationDesc">
              <el-input v-model="form.vehViolationDesc" type="textarea" placeholder="请输入内容"/>
            </el-form-item>
          </el-col>
          <el-col :md="12" :sm="12">
            <el-form-item label="备注" prop="remark">
              <el-input v-model="form.remark" type="textarea" placeholder="请输入内容"/>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button v-if="active==1" @click="()=>{
          active--
        }">上一步
        </el-button>
        <el-button type="primary" @click="submitForm" :disabled="active==0&&openType=='add'">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {addViolation, delViolation, getViolation, listViolation, updateViolation} from "@/api/merchant/violation";
import {storesRoleList} from "@/api/merchant/stores";
import {listCar} from "@/api/merchant/car";
import {listVhiceodr} from "@/api/merchant/order";

export default {
  name: "Violation",
  dicts: ['violation_status', 'violation_handler_method', 'order_servicetype'],
  data() {
    return {
      active: 0,
      branchOptions: [],
      carList: [],
      advanced: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 车辆违章信息表格数据
      violationList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,

      // 定在修改
      openType: '',

      order: {
        loading: true,
        total: 0,
        vehorderList: []
      },
      queryParamsOrder: {
        rangStatus: 'RECEIVE_LOADING,USE_LOADING',
        pageNum: 1,
        pageSize: 5,
      },
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        vehicleId: null,
        orderId: null,
        vehViolationDesc: null,
        carNum: null,
        status: null,
        violationTime: null,
        orderNum: null,
        vehViolationPics: null,
        vioHandledPics: null,
        handleMethod: null,
        entryName: null,
        confirmName: null,
        entryUserId: null,
        confirmUserId: null,
        deptId: null,
        wechatUserId: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        deductPoints: [
          {required: true, message: "金额为必填项", trigger: "blur"},
          {min: 1, type: 'number', message: "分数不能为零或负数", trigger: "blur"},
          {max: 12, type: 'number', message: "违章分数不可能超过12分", trigger: "blur"},
        ],
        money: [
          {required: true, message: "金额为必填项", trigger: "blur"},
          {
            pattern: /^[1-9]+\d*$/,
            message: "请输入正整数",
            trigger: "blur"
          }
        ],
        deptId: [
          {required: true, message: "门店组织架构id不能为空", trigger: "blur"}
        ],
        createTime: [
          {required: true, message: "创建时间不能为空", trigger: "blur"}
        ]
      }
    };
  },
  created() {
    this.getList();
    this.init()
  },
  methods: {
    init() {
      listCar({pageNum: 1, pageSize: 500}).then(res => {
        this.carList = res.rows
      })
      storesRoleList({
        pageNum: 1,
        pageSize: 100,
      }).then(res => {
        if (res.code == 200) {
          this.branchOptions = res.rows
        }
      })
    },
    toggleAdvanced() {
      this.advanced = !this.advanced
    },
    choseCar(info) {
      console.log(info)
      this.$set(this.form, 'deptId', Number(info.deptId))
      this.$set(this.form, 'carNum', info.carNum)
      this.$set(this.form, 'vehicleId', info.id)
    },
    /** 查询车辆违章信息列表 */
    getList() {
      this.loading = true;
      listViolation(this.queryParams).then(response => {
        this.violationList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        money: '',
        deductPoints: '',
        id: null,
        vehicleId: null,
        orderId: null,
        vehViolationDesc: null,
        carNum: null,
        status: 0,
        violationTime: null,
        orderNum: null,
        remark: null,
        vehViolationPics: null,
        vioHandledPics: null,
        handleMethod: null,
        entryName: null,
        confirmName: null,
        entryUserId: null,
        confirmUserId: null,
        deptId: null,
        wechatUserId: null,
        createTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    getOrderList() {
      listVhiceodr(this.queryParamsOrder).then(res => {
        this.order.loading = false
        if (res.code === 200) {
          this.order.total = res.total;
          this.order.vehorderList = res.rows
        }
      })
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.getOrderList()
      this.open = true;
      this.openType = 'add'
      this.title = "添加车辆违章信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getViolation(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.openType = 'edit'
        this.title = "修改车辆违章信息";
      });
    },
    choseOrder(row) {
      this.form.orderId = row.id
      this.form.orderNum = row.orderNum
      this.choseCar(row)
      this.active++
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateViolation(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.type = null
              this.getList();
            });
          } else {
            addViolation(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.type = null
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除车辆违章信息编号为"' + ids + '"的数据项？').then(function () {
        return delViolation(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('merchant/violation/export', {
        ...this.queryParams
      }, `violation_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>

<style>
.float-left {
  float: left;
}

.float-right {
  float: right;
}
</style>
